package cn.develop.server.commons;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import static cn.develop.server.commons.ResVO.ResCode.*;

/**
 * @author 1.
 * @version 1.0
 * @description: 返回结果封装类
 * @date 2024/2/29 22:18
 */
@Data
@AllArgsConstructor
public class ResVO<T> {

    private String code;
    private String msg;
    private T data;


    enum ResCode {

        OK("200", "请求成功！"),
        WARNING("201", "请求有误！"),
        ERROR("400", "请求出错！");

        private String code;
        private String desc;

        ResCode(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public String getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }
    }

    public static ResVO<?> ok() {
        return new ResVO<>(OK.code, OK.desc, null);
    }

    public static <D> ResVO<D> ok(D data) {
        return new ResVO<>(OK.code, OK.desc, data);
    }

    public static ResVO<?> warn(String msg) {
        return new ResVO<>(WARNING.code, WARNING.desc, msg);
    }

    public static ResVO<?> error(String msg) {
        return new ResVO<>(ERROR.code, ERROR.desc, msg);
    }

}
